package com.whyx.project.openapi.util;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * 字符串转换工具
 */
public class StringCls {
	
	public static String StrToHex(String str,boolean mSpace){
		return StrToHex(str,mSpace,"utf-8");
	}
	
	public static String StrToHex(String str,boolean mSpace,String charsetname)
	{ 
		if(str == null) return null; 
		try {
			return Bytes2HexString(str.getBytes(charsetname),mSpace);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null ;
		}
	} 
	
	public static String HexToStr(String str){
		return HexToStr(str,"utf-8");
	}

	public static String HexToStr(String str,String charsetname) 
	{ 
		if(str == null) return null; 
		try {
			return (new String(HexString2Bytes(str,false),charsetname));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	} 
	
	public static String Bytes2HexString(byte[] b,boolean mSpace) { 
		String ret = ""; 
		for (int i = 0; i < b.length; i++) { 
			String hex = Integer.toHexString(b[i] & 0xFF); 
			if (hex.length() == 1) { 
			hex = '0' + hex; 
			} 
			ret += hex.toUpperCase(); 
			if(mSpace)ret+=" ";
		} 
		return ret; 
	}
	
	/** 
	* 将指定字符串src，以每两个字符分割转换为16进制形式 
	* 如："2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9} 
	* @param src String 
	* @return byte[] 
	*/
	public static byte[] HexString2Bytes(String src,boolean mSpace){ 
		if(mSpace)src=src.replace(" ", "");
		byte[] ret = new byte[src.length()/2]; 
		byte[] tmp = src.getBytes(); 
		for(int i=0; i<ret.length; i++){ 
		ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); 
		} 
		return ret; 
	} 

	/** 
	* 将两个ASCII字符合成一个字节； 
	* 如："EF"--> 0xEF 
	* @param src0 byte 
	* @param src1 byte 
	* @return byte 
	*/ 
	public static byte uniteBytes(byte src0, byte src1){
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
        _b0 = (byte)(_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
        byte ret = (byte)(_b0 | _b1);
        return ret;
    }
	
	public static int ByteToUint(byte b){
		int i=(int)b;
		if(i<0)i=256+i;
		return i;		
	}

	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str))
			return true;
		else
			return false;
	}

	public static String completeStr(String str,int length,char c) {
		while(str.length()<length){
			str = c + str;
		}
		return str;
	}

	public static Date utcToLocal(String utcTime){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//若UTC时间未+8小时，则此处放开
		//sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
		Date utcDate = null;
		try {
			utcDate = sdf.parse(utcTime.substring(0,19).replace("T"," "));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		sdf.setTimeZone(TimeZone.getDefault());
		Date locatlDate = null;
		String localTime = sdf.format(utcDate.getTime());
		try {
			locatlDate = sdf.parse(localTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return locatlDate;
	}

	//单体测试
	public static void main111(String[] args) {

	}

}
